// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.

namespace System.Data.Entity.Core.Common.EntitySql.AST
{
    using System.Data.Entity.Utilities;
    using System.Diagnostics;

    // <summary>
    // Represents invocation expression: expr(...)
    // </summary>
    internal sealed class MethodExpr : GroupAggregateExpr
    {
        private readonly Node _expr;
        private readonly NodeList<Node> _args;
        private readonly NodeList<RelshipNavigationExpr> _relationships;

        // <summary>
        // Initializes method ast node.
        // </summary>
        internal MethodExpr(
            Node expr,
            DistinctKind distinctKind,
            NodeList<Node> args)
            : this(expr, distinctKind, args, null)
        {
        }

        // <summary>
        // Initializes a method ast node with relationships.
        // </summary>
        internal MethodExpr(
            Node expr,
            DistinctKind distinctKind,
            NodeList<Node> args,
            NodeList<RelshipNavigationExpr> relationships)
            : base(distinctKind)
        {
            DebugCheck.NotNull(expr);
            Debug.Assert(args == null || args.Count > 0, "args must be null or a non-empty list");

            _expr = expr;
            _args = args;
            _relationships = relationships;
        }

        // <summary>
        // For the following expression: "a.b.c.Xyz()", returns "a.b.c.Xyz".
        // </summary>
        internal Node Expr
        {
            get { return _expr; }
        }

        // <summary>
        // Argument list.
        // </summary>
        internal NodeList<Node> Args
        {
            get { return _args; }
        }

        // <summary>
        // True if there are associated relationship expressions.
        // </summary>
        internal bool HasRelationships
        {
            get { return null != _relationships && _relationships.Count > 0; }
        }

        // <summary>
        // Optional relationship list.
        // </summary>
        internal NodeList<RelshipNavigationExpr> Relationships
        {
            get { return _relationships; }
        }
    }
}
